<template>
  <div class="nav-bar" :class="{'is_fixed': isFixed}">
    <div class="container">
      <div class="pro-title">
        {{title}}
      </div>
      <div class="pro-param">
        <a href="javascript:;">概述<span>|</span></a>
        <a href="javascript:;">参数<span>|</span></a>
        <a href="javascript:;" class="user">用户评价</a>
        <slot name="buy"></slot>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'nav-bar',
    props: {
      title: String
    },
    data () {
      return {
        isFixed: false
      }
    },
    mounted() {
      // 事件名称 + 方法
      window.addEventListener('scroll', this.initHeight)
    },
    methods: {
      initHeight () {
        let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
        this.isFixed = scrollTop > 152 ? true:false;
      }
    },
    destroyed() {
      // 事件名称 + 方法 + 冒泡 (默认是true捕获)
      window.removeEventListener('scroll', this.initHeight, false)
    },
  }
</script>

<style lang='scss' scoped>
  .nav-bar {
    height: 70px;
    line-height: 70px;
    border-top: 1px solid #e5e5e5;
    background-color: #ffffff;
    box-shadow: 0 5px 5px #cccccc;
    z-index: 10;
    // 组件吸顶功能的实现
    &.is_fixed {
      position: fixed;
      top: 0;
      width: 100%;
    }
    .container {
      display: flex;
      align-items: center;
      justify-content: space-between;
      .pro-title {
        font-size: 18px;
        color: #333333;
        font-weight: bold;
      }
      .pro-param {
        font-size: 14px;
        a {
          color: #666666;
          span {
            padding: 0 10px
          }
        }
        .user {
          margin-right: 10px;
        }
      }
    }
  }
</style>
